<?php
	defined('RUN') or die('No access');

	class SitemapController
	{
		public function index()
		{
			header("Content-Type: text/xml; charset=UTF-8");
			
			$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
			$rssfeed .= '<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
			
			$rssfeed .= '<url>';
			$rssfeed .= '<loc><![CDATA[' . Url::base() . ']]></loc>';
			$rssfeed .= '<lastmod><![CDATA[' . gmdate("D, d M Y H:i:s",time()-86400) . ']]></lastmod>';
			$rssfeed .= '<changefreq>daily</changefreq>';
			$rssfeed .= '<priority>1.0</priority>';
			$rssfeed .= '</url>';

			$categories = Model::get('category')->getAll();
			foreach ($categories as $cat)
			{
				$rssfeed .= '<url>';
				$rssfeed .= '<loc><![CDATA[' . Url::anchor(String::toUrl($cat['name_vi']) . "-p" . $cat['id']) . ']]></loc>';
				$rssfeed .= '<lastmod><![CDATA[' . gmdate("D, d M Y H:i:s",time()-86400) . ']]></lastmod>';
				$rssfeed .= '<changefreq>daily</changefreq>';
				$rssfeed .= '<priority>0.8</priority>';
				$rssfeed .= '</url>';
			}

			$entries = Model::get('post')->getLastest(0);
			foreach ($entries as $entry)
			{
				$rssfeed .= '<url>';
				$rssfeed .= '<loc><![CDATA[' . Url::anchor(String::toUrl($entry['title']) . "-p" . $entry['id']) . ']]></loc>';
				$rssfeed .= '<lastmod><![CDATA[' . gmdate("D, d M Y H:i:s",time()-86400) . ']]></lastmod>';
				$rssfeed .= '<changefreq>daily</changefreq>';
				$rssfeed .= '<priority>0.7</priority>';
				$rssfeed .= '</url>';
			}
			
			$rssfeed .= '</urlset>';
		 	
		 	echo $rssfeed;
		}
	}
